package com.lgl.sqlSession.impl;

import com.lgl.prjo.Configuration;
import com.lgl.prjo.MappedStatement;
import com.lgl.sqlSession.ISqlSession;

import java.beans.IntrospectionException;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;

public class DefaultSqlSession implements ISqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws InvocationTargetException, SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, IntrospectionException, ClassNotFoundException {
        //将要去完成对simpleExcutor里的query方法的调用
        simpleExecutor simpleExecutor = new simpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<Object> queryList = simpleExecutor.query(configuration, mappedStatement, params);
        return (List<E>) queryList;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws SQLException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException, IntrospectionException, ClassNotFoundException {
        List<Object> objects = selectList(statementId, params);
        if (objects.size() == 1) {
            return (T) objects.get(0);
        } else  {
            throw new RuntimeException("查询结果为空或者返回结果更多");
        }

    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        //使用JDK动态代理来为Dao接口生成代理对象，并返回

        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[] {mapperClass
        }, new InvocationHandler() {
            //底层还是去执行JDBC代码
            //准备参数1.statementId
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className + "." + methodName;
                Type genericReturnType = method.getGenericReturnType();

                /* 返回类型是否是集合 */
                if (genericReturnType instanceof  ParameterizedType) {
                    List<Object> objects = selectList(statementId, args);
                    return  objects;
                } else {
                    Class<?> returnType = method.getReturnType();
                    String type = returnType.getTypeName();
                    if (type.equals("int")) {
                        return update(statementId, args);
                    } else {
                        Object object = selectOne(statementId, args);
                        return object;
                    }
                }
            }
        });

        return (T) proxyInstance;
    }

    @Override
    public int update(String statementId, Object... params) throws ClassNotFoundException, SQLException, NoSuchFieldException, IllegalAccessException {
        simpleExecutor simpleExecutor = new simpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        return simpleExecutor.update(configuration, mappedStatement, params);
    }
}
